package com.area.cth.controller;

import com.area.cth.pojo.Manage;
import com.area.cth.common.Result;
import com.area.cth.service.ManageService;
import com.area.cth.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "求购相关接口")
@RestController
@RequestMapping("/manage")
@CrossOrigin
public class ManageController {
    @Autowired
    private ManageService ms;

    @Operation(summary = "新增求购")
    @PostMapping
    public Result addManage(@RequestBody Manage m) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer launchUId = (Integer) map.get("id");
        Integer receiveUId = m.getReceiveUser();
        if (launchUId != receiveUId) {
            ms.addManage(m);
            return new Result(0,"发送成功",null);
        } else {
            return Result.error("不能向自己发送请求");
        }
    }

    @Operation(summary = "查询个人接收的求购")
    @GetMapping("/receive")
    public Result<List<Manage>> getReceiveManage() {
        List<Manage> receiveManage = ms.getReceiveManage();
        return Result.success(receiveManage);
    }

    @Operation(summary = "查询个人发起的求购")
    @GetMapping("/launch")
    public Result<List<Manage>> getLaunchManage() {
        List<Manage> launchManage = ms.getLaunchManage();
        return Result.success(launchManage);
    }

    @Operation(summary = "修改求购状态")
    @PutMapping
    public Result updateState(String manageState, Integer mId) {
        ms.updateState(manageState, mId);
        return Result.success(manageState);
    }

    @Operation(summary = "删除求购")
    @DeleteMapping
    public Result deleteManage(Integer mId) {
        ms.deleteManage(mId);
        return Result.success();
    }

    @Operation(summary = "根据商品id查询是否有同意的求购")
    @GetMapping("/buyIf")
    public Result<Integer> agreeCount(Integer gId) {
        Integer count = ms.agreeCount(gId);
        return Result.success(count);
    }
}
